<meta charset="UTF-8">
<?php
require ('./lib/init.php');
$art_id = $_GET['art_id'];
//判断地址栏传来的 art_id 是否合法
if (!is_numeric($art_id)){
    error ('文章id不合法！');
}

////文章是否存在
$sql = "select * from art where art_id = $art_id";
if (!mGetrow($sql)){
    error('文章不存在');
}

////查询出所有栏目
$sql = "select * from cat";
$cats = mGetAll($sql);
if (empty($_POST)){
    $sql = "select title,content,cat_id,arttag from art where art_id = $art_id";
    $art = mGetRow($sql);
    include (ROOT . '/view/admin/artedit.html');
}else{
    //    检测标题是否为空
    $art['title'] = trim($_POST['title']);
    if ($art['title'] == ''){
        error('标题不能为空');
    }
//    检测栏目是否合法
    $art['cat_id']= $_POST['cat_id'];
    if (!is_numeric($art['cat_id'])){
        error('栏目不合法！');
    }
//    检测内容是否为空
    $art['content'] = trim($_POST['content']);
    if ($art['content'] == ''){
        error('内容不能为空！');
    }
//    最后一次修改时间
    $art['lastup'] = time();
    if (!mExec('art',$art,'update'," art_id = $art_id ")){
            error('修改失败');
    }else{
        //            删除原表中所有的tag
        $sql = "delete from tag where art_id = $art_id";
        if (mQuery($sql)){
            //        获取新的tag
            $tag = explode(',',$_POST['tag']);
            $sql = "insert into tag (art_id,tag) values  ";
            foreach ($tag as $v){
                $sql .= "(" . $art_id . ",' " . $v . "'),";
            }
            $sql = rtrim($sql,",");
            if (mQuery($sql)){
                $sql = "update art set arttag = '$_POST[tag]' where art_id = $art_id";
                if (mQuery($sql)){
                    succ ('修改成功！');
                }
                succ('修改成功！');
            }else{
                error ('文章内容修改成功，但是标签修改失败。');
            }
        }else{
            error ('文章内容修改成功，但是标签修改失败。');
        }

    }
}


?>
